clear
insheet using "fromRA/Storting1915_Anna_Gomez_5.txt"

/* CATHCING TWO TYPOS */

replace svotesr2c1=30 if knr==512 /* LISTED AS 37, BUT THAT APPEARS TO BE NEIGHBORING MUNICIP'S RETURNS */
replace vvotesr1c3=11 if knr==1826 /* LISTED AS 57, BUT THAT APPEARS TO BE NEIGHBORING MUNICIP'S RETURNS */

drop if valgkrets==.
gen vvotesr2c3=.

*replace knavn="Kopervik Herred" if knr==1148
replace knavn="Kopervik herred" if knr==1148
replace knavn="Kopervik ladestad" if knr==1105  /* LADESTED MEANS CITY */

sort knavn
merge knavn using dta/knavn3
drop if _merge==2


foreach round in 1 2 {
		
		foreach var in svotesr`round'c1 svotesr`round'c2 avotesr`round'c1 avotesr`round'c2 v_avotesr`round'c1 v_avotesr`round'c2 vvotesr`round'c1 vvotesr`round'c2 vvotesr`round'c3 lvotesr`round'c1 lvotesr`round'c2 tvotesr`round'c1 tvotesr`round'c2 kirvotesr`round'c1 kirvotesr`round'c2 fvvotesr`round'c1 fvvotesr`round'c2 h_fvvotesr`round'c1 h_fvvotesr`round'c2 hvotesr`round'c1 hvotesr`round'c2 sprvotesr`round'c1 sprvotesr`round'c2 othvotesr`round'c1 othvotesr`round'c2 {
		replace `var'=0 if `var'==.
		}
	gen totalvotes`round'=svotesr`round'c1+svotesr`round'c2+avotesr`round'c1+avotesr`round'c2+v_avotesr`round'c1+v_avotesr`round'c2+vvotesr`round'c1+vvotesr`round'c2+vvotesr`round'c3+lvotesr`round'c1+lvotesr`round'c2+tvotesr`round'c1+tvotesr`round'c2+kirvotesr`round'c1+kirvotesr`round'c2+fvvotesr`round'c1+fvvotesr`round'c2+h_fvvotesr`round'c1+h_fvvotesr`round'c2+hvotesr`round'c1+hvotesr`round'c2+sprvotesr`round'c1+sprvotesr`round'c2+othvotesr`round'c1+othvotesr`round'c2
}
li knr knavn* _merge totalvotes2 if valgkrets==501


foreach round in 1{
	foreach party in s v a v_a t kir fv h_fv h oth {
			foreach cand in 1 {
		li knr valgkrets `party'hometownr`round'c`cand' `party'occupationr`round'c`cand' `party'votesr`round'c`cand' if `party'occupationr`round'c`cand'=="" & `party'votesr`round'c`cand'>0
		}
	}
}


foreach round in 2{
	foreach party in s v v_a fv h_fv h oth {
		foreach cand in 1 {
		li knr valgkrets `party'hometownr`round'c`cand' `party'occupationr`round'c`cand' `party'votesr`round'c`cand' if `party'occupationr`round'c`cand'=="" & `party'votesr`round'c`cand'>0
		}
	}
}


/* HOMETOWN VARIABLES */

foreach round in 1 2 {
	foreach party in s v a v_a l t kir fv h_fv h oth {
		foreach cand in 1 2{
		gen match`party'r`round'c`cand'=0
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn2
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn3
		replace match`party'r`round'c`cand'=. if `party'hometownr`round'c`cand'==""
		replace match`party'r`round'c`cand'=. if `party'votesr`round'c`cand'==.
		egen match`party'r`round'c`cand'_di=sum(match`party'r`round'c`cand'), by(valgkrets)
		replace match`party'r`round'c`cand'_di=. if `party'votesr`round'c`cand'==.
		}
	}
}
		
foreach round in 1 {
	foreach party in v {
			foreach cand in 3 {
		gen match`party'r`round'c`cand'=0
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn2
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn3
		replace match`party'r`round'c`cand'=. if `party'hometownr`round'c`cand'==""
		replace match`party'r`round'c`cand'=. if `party'votesr`round'c`cand'==.
		egen match`party'r`round'c`cand'_di=sum(match`party'r`round'c`cand'), by(valgkrets)
		replace match`party'r`round'c`cand'_di=. if `party'votesr`round'c`cand'==.
		}
	}
}
		
			
sort valgkrets knr			
*br knavn valgkrets matchsr1c1 matchsr1c1_di shometownr1c1	
*br knavn valgkrets matchsr1c1 matchsr1c1_di shometownr1c1 if matchsr1c1_di==0 & shometownr1c1!=" "
*br knavn valgkrets matchvr1c1 matchvr1c1_di vhometownr1c1 if matchvr1c1_di==0 & vhometownr1c1!=""
*br knavn valgkrets matchh_fvr1c1 matchh_fvr1c1_di h_fvhometownr1c1 if matchh_fvr1c1_di==0 & h_fvhometownr1c1!=""


gen year=1915
save dta/import1915, replace

/* CREATING DATA SET ON THE VALGKRETS LEVEL */

collapse (sum) *votes* (mean) rejectedelection city, by(valgkrets)

/* CREATING VOTE SHARE VARIABLES */


foreach candidate in svotesr1c1 svotesr1c2 avotesr1c1 avotesr1c2 v_avotesr1c1 v_avotesr1c2 vvotesr1c1 vvotesr1c2 vvotesr1c3 lvotesr1c1 lvotesr1c2 tvotesr1c1 tvotesr1c2 kirvotesr1c1 kirvotesr1c2 fvvotesr1c1 fvvotesr1c2 h_fvvotesr1c1 h_fvvotesr1c2 hvotesr1c1 hvotesr1c2 sprvotesr1c1 sprvotesr1c2 othvotesr1c1 othvotesr1c2 {
gen `candidate'share1=`candidate'/totalvotes1
}

foreach candidate in svotesr2c1 svotesr2c2 avotesr2c1 avotesr2c2 v_avotesr2c1 v_avotesr2c2 vvotesr2c1 vvotesr2c2 vvotesr2c3 lvotesr2c1 lvotesr2c2 tvotesr2c1 tvotesr2c2 kirvotesr2c1 kirvotesr2c2 fvvotesr2c1 fvvotesr2c2 h_fvvotesr2c1 h_fvvotesr2c2 hvotesr2c1 hvotesr2c2 sprvotesr2c1 sprvotesr2c2 othvotesr2c1 othvotesr2c2 {
gen `candidate'share2=`candidate'/totalvotes2
}

foreach round in 1 2 {

	gen maxvotesh`round'=0
	foreach candidate in svotesr`round'c1 svotesr`round'c2 avotesr`round'c1 avotesr`round'c2 v_avotesr`round'c1 v_avotesr`round'c2 vvotesr`round'c1 vvotesr`round'c2 vvotesr`round'c3 lvotesr`round'c1 lvotesr`round'c2 tvotesr`round'c1 tvotesr`round'c2 kirvotesr`round'c1 kirvotesr`round'c2 fvvotesr`round'c1 fvvotesr`round'c2 h_fvvotesr`round'c1 h_fvvotesr`round'c2 hvotesr`round'c1 hvotesr`round'c2 sprvotesr`round'c1 sprvotesr`round'c2 othvotesr`round'c1 othvotesr`round'c2 {
	replace maxvotesh`round'=`candidate'share`round' if `candidate'share`round'>maxvotesh`round'
	}
}



gen year=1915

sort valgkrets year
merge valgkrets year using dta/SMD

keep if _merge==3
*scatter approvedvotes1 totalvotes1, mlabel(valgkrets)
*scatter approvedvotes2 totalvotes2, mlabel(valgkrets)

gen zz=approvedvotes1-totalvotes1
gen zz2=approvedvotes2-totalvotes2

li valgkrets approvedvotes1 totalvotes1 zz if zz!=0
li valgkrets approvedvotes2 totalvotes2 zz2 if zz2!=0

li valgkr if maxvotesh1>0.5 & maxvotesh2!=.

